﻿$(document).ready(function () {
    Calcurate.init(),
    Calcurate.genBankInfoList()
});
var addCount = 1;
var Calcurate = {
    tipElements: [],
    init: function () {
        Calcurate.initEvent()
    },
    initEvent: function () {
        $(".code-p").on("click", Calcurate.genAuthCodeImg);
        $(".btn-submit").on("click", Calcurate.postData);
        $(".btn-action").on("click", Calcurate.onAddTR);
        $(".btn-del").attr("onClick", "Calcurate.getDel(this)")
    },
    genAuthCodeImg: function () {
        var a = Helper.genAuthCode(21);
        $("#authCodeImg").attr("src", a)
    },
    postData:function(){
        var bankCardTypes = $("bankCardType");
        var bankCardNos = $("bankCardNo");
        var cardNames = $("cardName");
        var amounts = $("amount");
    },

    postData: function () {
        var b = $.trim($("#authCode").val());
        if (b == "" || b == null) {
            Helper.alert("验证码不能为空");
            return false
        }
        var q = document.getElementsByName("bankCardType");
        var c = document.getElementsByName("bankCardNo");
        var g = document.getElementsByName("cardName");
        var a = document.getElementsByName("openBank");
        var k = document.getElementsByName("amount");
        var h = [];
        var m = [];
        var f = [];
        var o = [];
        var r = [];
        for (var j = 0; j < q.length; j++) {
            var p = q[j].value;
            h.push(p)
        }
        for (var j = 0; j < c.length; j++) {
            var n = c[j].value;
            if (n == null || "" == n) {
                Helper.alert("卡号不能为空");
                return
            } else {
                var l = /^\d*$/;
                if (!l.exec(n)) {
                    Helper.alert("银行卡号必须全为数字");
                    return
                }
            }
            if (g[j].value.indexOf("公司") < 0 && q[j].value != "CSCB" && !PayCommon.checkBankCardNo(n)) {
                Helper.alert("银行卡号：【" + n + "】不合法，请重新输入");
                return
            }
            m.push(n)
        }
        for (var j = 0; j < g.length; j++) {
            if (g[j].value == null || "" == g[j].value) {
                Helper.alert("姓名不能为空");
                return
            }
            o.push(g[j].value)
        }
        var e = 0;
        for (var j = 0; j < k.length; j++) {
            var p = k[j].value;
            e = parseFloat(e) + parseFloat(p);
            if ("" == k[j].value) {
                Helper.alert("金额不能为空");
                return
            } else {
                var d = /^\d+(\.\d{1,2})?$/;
                if (!d.test(k[j].value)) {
                    Helper.alert("金额必须是整数,或者是带两位小数的数字");
                    return
                }
                if (parseFloat(k[j].value) == 0) {
                    Helper.alert("金额必须大于0");
                    return
                }
            }
            r.push(p)
        }
        $(".btn-primary").attr("disabled", "true");
        $.ajax({
            type: "POST",
            data: {
                cardType: h.join(","),
                cardNo: m.join(","),
                cardName: o.join(","),
                openBank: f.join(","),
                amount: r.join(","),
                totalAmount: e,
                authCode: b
            },
            url: "",
            cache: false,
            dataType: "json",
            success: function (i) {
                $(".btn-primary").removeAttr("disabled");
                if (i.code == 1) {
                    $.ajax({
                        type: "POST",
                        url: Helper.getRootPath() + "/batchPay.aspx",
                        data: {
                            cardType: h.join(","),
                            cardNo: m.join(","),
                            cardName: o.join(","),
                            openBank: f.join(","),
                            amount: r.join(",")
                        },
                        cache: false,
                        dataType: "json",
                        success: function (s) {
                            //if (s.code == 1) {
                            //    location.href = Helper.getWebRootPath() + "/batchPay/listBatchPay.html?source=1&batchId=" + s.batchId
                            //} else {
                            //    Helper.confirm_one(s.errorMsg, function () {
                            //        location.href = Helper.getWebRootPath() + "/batchPay/calcurate.html"
                            //    })
                            //}
                        }
                    })
                } else {
                    Helper.alert(i.msg);
                    Calcurate.genAuthCodeImg()
                }
            }
        })
    },
    onAddTR: function () {
        addCount++;
        $("#dt_a tbody").append("<tr><td  class='td-bank'>    <select name='bankCardType' class='bank'></select></td><td class='td-name'>    <input type='text' name='cardName' maxlength='64' class='ipt ipt-sm'/></td> <td  class='td-card'>    <input type='text' name='bankCardNo' maxlength='19' class='ipt card-number' /></td><td class='td-money'>    <input type='text' name='amount' maxlength='12' class='ipt ipt-sm' /></td><td class='td-oper'>    <a class='btn btn-del' href='#'>删除</a></td></tr>");
        Calcurate.genBankInfoList();
        $(".btn-del").attr("onClick", "Calcurate.getDel(this)")
    },
    getDel: function (a) {
        if (addCount > 1) {
            $(a).parent().parent().remove();
            addCount--
        }
    },
    genBankInfoList: function () {
        $.ajax({
            type: "get",
            url: "/pub/listBankInfo.ashx",
            data: {
                enableRemit: "1"
            },
            cache: false,
            dataType: "json",
            success: function (c) {
                var b = c;
                for (var a = 0; a < b.length; a++) {
                    $("[name='bankCardType']").append("<option value='" + b[a].BankCode + "'>" + b[a].BankName + "</option>")
                }
            }
        })
    }
};